/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.garagesale.user.web.action;

import com.garagesale.data.DBAccess;
import com.garagesale.data.Item;
import com.garagesale.data.User;
import com.garagesale.user.web.form.UserForm;
import com.garagesale.web.util.WebAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author christophe
 */
public class ViewUserAction extends org.apache.struts.action.Action
{

    /* forward name="success" path="" */
    private static final String SUCCESS = "view";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {

        UserForm userForm = (UserForm) form;

        Integer userID = userForm.getUserID();
        User user = null;

        user = (User) request.getSession().getAttribute("user");


        if (null != request.getAttribute(Globals.ERROR_KEY))
        {
            ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY);



            if (!errors.isEmpty())
            {
                request.setAttribute("inError", "true");
                return mapping.findForward("edit");
            }

        }

        if (null == request.getUserPrincipal() && WebAction.CREATE.isAction(userForm.getAction()))
        {
            // make sure that the session is indeed empty
            request.getSession().removeAttribute("user");
            request.getSession().removeAttribute("cart");

            // this is new user that needs to be created
            return mapping.findForward("create");
        } else if (null == user)
        {
            // force the user to login
            return mapping.findForward("login");
        }

        if (!"admin".equals(user.getUserRole()))
        {
            userID = user.getUserID();
        }

        user = DBAccess.getInstance().retrieveByID(User.class, userID);
        request.getSession().setAttribute("user", user);

        if (WebAction.EDIT.isAction(userForm.getAction()))
        {
            // retrieve the user
            userForm.fromUser(user);
            return mapping.findForward("edit");
        }


        // go to the view page
        return mapping.findForward(SUCCESS);
    }
}
